<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/vue"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .container {
      position: absolute;
      width: 200px;
      height: 40px;
      background-color: pink;
    }
  </style>
</head>

<body>
  <div id="root"></div>
  <script>
    /* 
      1. v-pos 示例
        - 根据给定值，设置dom元素距离顶部的值
      2. 指令接收参数
      3. 通过data数据设置指令参数，并修改data值通过自定义指令更新DOM元素信息
      - updated钩子函数
      4. updated和mounted的简写形式
      5. :方式接收参数
        - binding.value 指令=接收的参数
        - binding.arg 指令:接收的参数
    */

    const app = Vue.createApp({
      template: `
      <div class="container" v-pos="topVal">
        
      </div>
    `,
      data() {
        return {
          topVal: 10
        }
      }
    })
    app.directive('pos', {
      mounted(el, binding) {
        el.style.top = (binding.value + 'px')
      },
      updated(el, binding) {
        el.style.top = (binding.value + 'px')
      }
    })
    // updated+mounted的简写形式
    app.directive('pos', (el, binding) => {
      el.style.top = (binding.value + 'px')
    })
    const vm = app.mount('#root');

  </script>
</body>

</html>